#ifndef	__MTCP_MODULE__H__
#define __MTCP_MODULE__H__
#pragma once

#include "FrameBase.h"
#include "TMHandler.h"


//socket
extern int Open(const char*  address = "127.0.0.1", int port = MTCP_PORT_DEFAULT, int timeout=5000);
extern void Close();

const char*  getVersion();

class C1SS :public CFrameBase
{
public:
	C1SS();
	~C1SS();

	int NTC_STEADY_STATE(float v);
	int T_STEADY_STATE(float v);
	int SOCKET_DCR(float v);
	int I_DR(float v);
	int V_FORWARD(const char*  v);
	int PEAK_OPTICAL_POWER(const char* v);
	int PEAK_WAVELENGTH(float v);
	int FWHM_WAVELENGTH(float v);
	int WL_DATA_LEN(float v);

	int setvalue(char*  what, char*  value);
	int SendC1SS();

private:
	tMTCP_payload_C1SS_REQ m_C1SS_REQ;
};


class DPCK :public CFrameBase
{
public:
	DPCK();
	~DPCK();
	int I_IDLE_LOW(float v);
	int NVM_DATA(const char*  d);	//1,2,3,4,5

	int setvalue(char*  what, char*  value);
	int SendDPCK();

private:
	tMTCP_payload_DPCK_REQ m_DPCK_REQ;
};


class DPWC :CFrameBase
{
public:
	DPWC();
	~DPWC();
	int NVM_DATA(const char*  data);

	int setvalue(char*  what, char*  value);
	int SendDPWC();

private:
	tMTCP_payload_DPWC_REQ m_DPWC_REQ;
};



class DPWR :public CFrameBase
{
public:
	DPWR();
	~DPWR();

	const char*  GetData();
	int SendDPWR();
private:
	tMTCP_payload_DPWR_RSP m_Rep;
	char  m_buffer[2048];
};

class TSCR :public CFrameBase
{
public:
	TSCR();
	~TSCR();

	int setDUTSN(const char*  sn);
	int setSocketSN(const char* sn);
	int setMode(int mode);
	int setSlotID(int slot);
	int SendTSCR();
	int setStationID(int station);
	int setSWVersion(const char* sw = "V1.0");

	int setvalue(char*  what, char*  value);

private:
	tMTCP_payload_TSCR_REQ m_TSCR_REQ;
};


class VCAL :public CFrameBase
{
public:
	VCAL();
	~VCAL();


	int NTC_STEADY_STATE(float v);
	int T_STEADY_STATE(float v);
	int PEAK_OPTICAL_POWER_FINAL(float v);
	int I_PULSE_FINAL(float v);
	int V_PULSE_FINAL(float v);
	int PEAK_WAVELENGTH(float v);
	int FWHM_WAVELENGTH(float v);
	int WL_DATA_LEN(int v);
	int ITER_CNT(int v);

	int pushData(float I, float v, float power, const char*  v_pulse_raw, float power_raw);

	int setvalue(char*  what, char*  value);
	int SendVCAL();
private:
	tMTCP_payload_VCAL_REQ m_VCAL_REQ;
	tMTCP_data_ADIP * m_pData;
};

class VDCR :public CFrameBase
{
public:
	VDCR();
	~VDCR();

	float I_DR();
	float T_SETTLE();

	
	int SendVDCR();
private:
	tMTCP_payload_VDCR_RSP m_Rep;
};

class VTST :public CFrameBase
{
public:
	VTST();
	~VTST();

	int NTC_INIT(float v);
	int PEAK_OPTICAL_POWER(const char*  data);
	int REVERSE_V(float voltage);
	int REVERSE_I(float current);
	int FORWARD_V(float v);
	int FORWARD_I(float v);
	int NTC_STEADY_STATE(float ntc);
	int T_STEADY_STATE(float time);
	int PEAK_WAVELENHTH(const char*  data);
	int FWHM_WAVELENGTH(const char*  data);
	int WL_DATA_CNT(uint8_t v);
	int WL_LEN(int v);

	//int setPayload(tMTCP_payload_VTST_REQ payload);
	int pushData(const char*  d1, const char*  d2);

	int pushWaveLength(const char*  wave);

	int setvalue(char*  what, char*  value);
	int SendVTST();

private:
	tMTCP_payload_VTST_REQ m_VTST_REQ;
	tMTCP_data_TTDP * m_pdata;

};

class TSED:public CFrameBase
{
public:
	TSED();
	~TSED();

	int SendTSED();
private:

};

///////////////////////////////////////////////////////////////////
//Chamber2
//////////////////////////////////////////////////////////////////
class EMCT:public CFrameBase
{
public:
	EMCT();
	~EMCT();

	int IMG_WIDTH(unsigned short v);
	int IMG_HEIGHT(unsigned short v);
	int IMG_SIZE(unsigned int v);
	int NTC_STEADY_STATE(float v);
	int CLUSTER_THRESHOLD(float v);
	int VALID_EMITTER_CNT(unsigned char  v);
	int EMITTER_CENTER_X(const char*  data);
	int EMITTER_CENTER_Y(const char*  data);
	int EMITTER_SIZE(const char*  data);
	int MAX_POWER(const char*  data);
	int MIN_POWER(const char*  data);
	int AVG_POWER(const char*  data);
	int STD_POWER(const char*  data);
	int I_DR(float v);
	int V_FOR(float v);

	int setvalue(char*  what, char*  value);
	int SendEMCT();

private:
	tMTCP_payload_EMCT_REQ m_EMCT_REQ;
};


//////////////////////////////////////////////////
//Chamber3
/////////////////////////////////////////////////
class BMPF:public CFrameBase
{
public:
	BMPF();
	~BMPF();

	int IMG_WIDTH(unsigned short v);
	int IMG_HEIGHT(unsigned short v);
	int IMG_SIZE(unsigned short v);
	int NTC_STEADY_STATE(float v);
	int TOTAL_OPTICAL_POWER(float v);
	int BEAM_SIZE(float v);
	int HOT_SPOT_POWER(float v);
	int HOT_SPOT_LOC_X(unsigned short v);
	int HOT_SPOT_LOC_Y(unsigned short v);
	int I_DR(float v);
	int V_FOR(float v);

	int setvalue(char*  what, char*  value);
	int SendBMPF();
private:
	tMTCP_payload_BMPF_REQ m_BMPF_REQ;
};


//////////////////////////////////////////////////
// POST
/////////////////////////////////////////////////
class POST : public CFrameBase
{
public:
	POST();
	~POST();

	int T_BIN()
	{
		return _rsp.T_BIN;
	}

	int T_ERRC()
	{
		return _rsp.T_ERRC;
	}

	const char*  T_ERRS()
	{
		return (const char* )_rsp.T_ERRS;
	}
	
	int SendPOST();
private:
	tMTCP_payload_POST_RSP	_rsp;
};



//////////////////////////////////////////////////
//void set_IMG_CNT(uint8_t IMG_CNT){ _req.IMG_CNT = IMG_CNT; }
//////////////////////////////////////////////////
//#define SET_PRO(type, name) void set_##name(type name){ _req.##name = name; }
#define SET_PRO(type, name) void name(type name){ _req.##name = name; }
#define SET_ARRAY_PRO(type, name, size) void set_##name(type name[size]){for (int i = 0; i < size; i++)_req.##name[i] = name[i];}

//////////////////////////////////////////////////
// C1ES
/////////////////////////////////////////////////
class C1ES : public CFrameBase
{
public:
	C1ES();
	~C1ES();


public:
	SET_PRO(int, IMG_CNT);
	SET_PRO(float, NTC_STEADY_STATE);
	SET_PRO(float, SCREEN_REFLECTANCE);
	SET_PRO(float, HOT_SPOT_THETA);
	SET_PRO(float, HOT_SPOT_PHI);
	SET_PRO(float, AE_POWER);
	SET_PRO(float, OPTICAL_POWER);

public:
	int get_payload_C1ES();
	int setvalue(char*  what, char*  value);
	int SendC1ES();

private:
	tMTCP_payload_C1ES_REQ		_req;
}; 


//////////////////////////////////////////////////
// FLDP
/////////////////////////////////////////////////
class FLDP : public CFrameBase
{
public:
	FLDP();
	~FLDP();

public:
	SET_PRO(int, IMG_CNT);
	SET_PRO(float, NTC_STEADY_STATE);
	SET_PRO(float, SCREEN_REFLECTANCE);
	SET_PRO(float, POWER_IN_FOV);
	SET_PRO(float, POWER_EFFICIENCY);
	SET_PRO(float, HFNU);
	SET_PRO(float, I_DR);
	SET_PRO(float, V_FOR);


	int LFNU_P_C(const char*  data)
	{
		char*  p = new char[strlen(data) + 1];
		strcpy(p, data);

		char*  dd;
		char*  k = strtok(p, ",");
		int index = 0;
		while (k)
		{
			_req.LFNU_P_C[index++] = atof(k);
			if (index > 9) break;
			k = strtok(NULL, ",");
		}
		return 0;
	}
	int OPTICAL_POWER_MAX(const char* data)	// Optical power max for each region
	{
		char*  p = new char[strlen(data) + 1];
		strcpy(p, data);

		char*  dd;
		char*  k = strtok(p, ",");
		int index = 0;
		while (k)
		{
			_req.OPTICAL_POWER_MAX[index++] = atof(k);
			if (index > 9) break;
			k = strtok(NULL, ",");
		}
		return 0;
	}
	int OPTICAL_POWER_MIN(const char* data)	// optical power min for each region
	{
		char*  p = new char[strlen(data) + 1];
		strcpy(p, data);

		char*  dd;
		char*  k = strtok(p, ",");
		int index = 0;
		while (k)
		{
			_req.OPTICAL_POWER_MIN[index++] = atof(k);
			if (index > 9) break;
			k = strtok(NULL, ",");
		}
		return 0;
	}
	int OPTICAL_POWER_AVG(const char* data)
	{
		char*  p = new char[strlen(data) + 1];
		strcpy(p, data);

		char*  dd;
		char*  k = strtok(p, ",");
		int index = 0;
		while (k)
		{
			_req.OPTICAL_POWER_AVG[index++] = atof(k);
			if (index > 9) break;
			k = strtok(NULL, ",");
		}
		return 0;
	}

public:
	int setvalue(char*  what, char*  value);
	int SendFLDP();

private:
	tMTCP_payload_FLDP_REQ		_req;
};



//////////////////////////////////////////////////
// FLDI
/////////////////////////////////////////////////
class FLDI : public CFrameBase
{
public:
	FLDI();
	~FLDI();

public:
	SET_PRO(int, IMG_INDEX);
	SET_PRO(int, IMG_WIDTH);
	SET_PRO(int, IMG_HEIGHT);
	SET_PRO(int, IMG_SIZE);
	SET_PRO(int, POS_X);
	SET_PRO(int, POS_Y);
	SET_PRO(float, POWER_TO_PIXEL_RATIO);

public:
	int setvalue(char*  what, char*  value);
	int SendFLDI();

private:
	tMTCP_payload_FLDI_REQ	_req;
};


//////////////////////////////////////////////////
// INIT
/////////////////////////////////////////////////
class INIT : public CFrameBase
{
public:
	INIT();
	~INIT();

public:

public:
	int SendINIT();

private:
	
	tMTCP_payload_INIT_REQ	_req;
};


//////////////////////////////////////////////////
// SEQU
/////////////////////////////////////////////////
class SEQU : public CFrameBase
{
public:
	SEQU();
	~SEQU();

public:
	int SendSEQU();

	int GetItemCount()
	{
		return _rsp.TEST_ITEM_CNT;
	}
	int GetChamber()
	{
		return _rsp.CHAMBER_CNT;
	}

	const char*  CHAMBER_TO_RUN()
	{
		// sprintf(CHAMBER_RUN,)
		// return CHAMBER_RUN;
		
		int i = 0;
		for (i = 0; i < _rsp.CHAMBER_CNT; i++)
		{
			_rsp.CHAMBER_TO_RUN[i] = '0' + _rsp.CHAMBER_TO_RUN[i];
		}
		
		_rsp.CHAMBER_TO_RUN[i] = 0;

		return (const char* )_rsp.CHAMBER_TO_RUN;
	}
private:
	tMTCP_payload_SEQU_RSP	_rsp;
	char*  CHAMBER_RUN[128];

};


///////////////////////////////////////////
//Chamber4
//////////////////////////////////////////



/////////////////////////////////////////////
//Ginbal
/////////////////////////////////////////////
class ALPR:CFrameBase
{
public:
	ALPR();
	~ALPR();
	float	HOT_SPOT_THETA()			// theta angle for hot spot
	{
		return m_ALPR_RSP.HOT_SPOT_THETA;
	}

	float	HOT_SPOT_PHI()
	{
		return m_ALPR_RSP.HOT_SPOT_PHI;
	}

	int SendALPR();

private:
	tMTCP_payload_ALPR_RSP m_ALPR_RSP;
};


class ALPH:CFrameBase
{
public:
	ALPH();
	~ALPH();
	SET_PRO(int, IMG_CNT);
	SET_PRO(int, IMG_WIDTH);
	SET_PRO(int, IMG_HEIGHT);
	SET_PRO(int, IMG_SIZE);
	SET_PRO(float, I_DR);
	SET_PRO(float, V_FOR);

	int SetImage(const char*  imagepath)
	{
		return 0;
	}
	int setvalue(char*  what, char*  value);
	int SendALPH();


private:
	tMTCP_payload_ALPH_REQ _req;
};

#endif